/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.base;

import br.ufba.lasid.base.api.IResource;
import br.ufba.lasid.ds.fd.FailureDetector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



/**
 *
 * @author aliriosa
 */
public class Resource implements IResource{
    private IResource parent;
    private String resourceID;
    private States state = States.Undefined;
    private Kinds kind = Kinds.Base;
    
    private static final Log logger = LogFactory.getLog(FailureDetector.class);

    public Resource(String resourceID){
        this(null, resourceID);
    }
    
    public Resource(IResource parent, String resourceID) {
        this.parent = parent;
        this.resourceID = resourceID;
    }
    
    
    @Override
    public String getID() {
        return this.resourceID;
    }

    @Override
    public String getAddress() throws Exception {
        String pAddress = "null";
        if(parent != null){
            pAddress = parent.getAddress();
        }
        return this.getID() + "@" + pAddress;
    }

    
    @Override
    public Kinds getKind() throws Exception {
        return kind;
    }
    
    @Override
    public void setKind(Kinds newKind) throws Exception {
        this.kind = newKind;
    }    

    @Override
    public IResource getParent() {
        return parent;
    }
    
    @Override
    public void setParent(IResource parent) {
        this.parent = parent;
    }
    

    @Override
    public void setState(States newState) {
        if(!this.state.equals(newState)){
            try {
                logger.info(
                    this.getAddress() + " is going to change " + 
                    "from " + this.state + " to " + newState
                );
            } catch (Exception ex) {
                //Logger.getLogger(Resource.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        this.state = newState;
    }

    @Override
    public States getState() {
        //this.updateState();
        return this.state;
    }

    @Override
    public boolean isUp() {
        return States.Up.equals(this.state);
    }

    @Override
    public boolean isDown() {
        return States.Down.equals(this.state);
    }

    @Override
    public boolean isUndefined() {
        return States.Undefined.equals(this.state);
    }
    
    @Override
    public void updateState() throws Exception {
        /**
         * this resource do not update state by itself.
         */
    }
    
}
